package com.cskaoyan.service.wx;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.MarketOrderGoods;
import com.cskaoyan.bean.MarketOrderGoodsExample;
import com.cskaoyan.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.model.vx.vo.UserIndexByChenVO;
import com.cskaoyan.model.vx.vo.WxUserOrderIndexVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: ChenZhuqi
 * @Date: 2021/12/2 17:08
 * @Description:
 */
@Service
public class WxUserIndexServiceImpl implements WxUserIndexService {
    @Autowired
    MarketOrderMapper orderMapper;

    @Autowired
    MarketOrderGoodsMapper orderGoodsMapper;

    @Override
    public UserIndexByChenVO userIndex() {
        UserIndexByChenVO userIndexVO = new UserIndexByChenVO();
        MarketOrderExample orderExample = new MarketOrderExample();
        WxUserOrderIndexVo wxUserOrderIndexVo = new WxUserOrderIndexVo();
        //未支付
        orderExample.createCriteria().andOrderStatusEqualTo((short) 101).andDeletedEqualTo(false);
        List<MarketOrder> unpaid = orderMapper.selectByExample(orderExample);
        wxUserOrderIndexVo.setUnpaid(unpaid.size());
        //未收货
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.createCriteria().andOrderStatusEqualTo((short) 301).andDeletedEqualTo(false);
        List<MarketOrder> unrecv = orderMapper.selectByExample(marketOrderExample);
        wxUserOrderIndexVo.setUnrecv(unrecv.size());
        // 未发货ship
        MarketOrderExample marketOrderExample1 = new MarketOrderExample();
        marketOrderExample1.createCriteria().andOrderStatusEqualTo((short) 201).andDeletedEqualTo(false);
        List<MarketOrder> unship = orderMapper.selectByExample(marketOrderExample1);
        wxUserOrderIndexVo.setUnship(unship.size());

        //未评论
        //查出用户的订单
        MarketOrderExample marketOrderExample2 = new MarketOrderExample();
        marketOrderExample2.createCriteria().andUserIdEqualTo(301).andDeletedEqualTo(false).andOrderStatusEqualTo((short) 401);
        List<MarketOrder> order = orderMapper.selectByExample(marketOrderExample2);
        //通过订单id查到商品
        MarketOrderGoodsExample orderGoodsExample = new MarketOrderGoodsExample();
        int i = 0;
        for (MarketOrder marketOrder : order) {
            orderGoodsExample.createCriteria().andOrderIdEqualTo(marketOrder.getId());
            List<MarketOrderGoods> marketOrderGoods = orderGoodsMapper.selectByExample(orderGoodsExample);
            for (MarketOrderGoods marketOrderGood : marketOrderGoods) {
                if (marketOrderGood.getComment().equals(0)) {
                    i++;
                }
            }
        }
        wxUserOrderIndexVo.setUncomment(i);

        userIndexVO.setOrder(wxUserOrderIndexVo);

        return userIndexVO;
    }
}
